package com.lagou.controller;

import com.lagou.domain.QueryVo;
import com.lagou.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
@SessionAttributes("username")
public class UserController {

    @RequestMapping("/quick")
    public String quick(){
        System.out.println("spring从入门到放弃");
        return "quick";
    }

    @RequestMapping("/simpleParam")
    public String simpleParam(Integer id,String username){
        System.out.println(id);
        System.out.println(username);
        return "quick";
    }

    @RequestMapping("/pojoParam")
    public String pojoParam(User user){
        System.out.println(user);
        return "quick";
    }

    @RequestMapping("/arrayParam")
    public String arrayParam(Integer[] id){
        System.out.println(Arrays.toString(id));
        return "quick";

    }

    @RequestMapping("/queryParam")
    public String queryParam(QueryVo queryVo){
        System.out.println(queryVo);
        return "quick";
    }
    @RequestMapping("/converterParam")
    public String queryParam(Date birthday){
        System.out.println(birthday);
        return "quick";
    }

    @RequestMapping("/findByPage")
    public String findByPage(@RequestParam(name="pageNo") Integer pageNum,
                             @RequestParam(defaultValue = "5") Integer pageSize){
        System.out.println(pageNum);
        System.out.println(pageSize);
        return "quick";
    }
    @RequestMapping("/requestHead")
    public String requestHead(@RequestHeader("cookie") String cookie){
        System.out.println(cookie);
        return "quick";
    }

    @RequestMapping("/cookieValue")
    public String cookieValue(@CookieValue("JSESSIONID") String jsseionId){
        System.out.println(jsseionId);
        return "quick";
    }

    /**
     * 转发
     */
    @RequestMapping("/forward")
    public String forward(Model model){
        model.addAttribute("username","欢迎使用");
        return "forward:/WEB-INF/pages/quick.jsp";
    }

    @RequestMapping("/returnString")
    public String returnString(){
        return "quick";
    }

    @RequestMapping("/redirect")
    public String redirect(Model model){
        model.addAttribute("username","欢迎使用");
        return "redirect:/index.jsp";
    }

    @RequestMapping("/returnModelAndView")
    public ModelAndView returnModelAndView(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("username","modelAndView方式一");
        modelAndView.setViewName("quick");
        return modelAndView;
    }

    @RequestMapping("/returnModelAndView2")
    public ModelAndView returnModelAndView2(ModelAndView modelAndView){
        modelAndView.addObject("username","modelAndView方式二");
        modelAndView.setViewName("quick");
        return modelAndView;
    }

    @RequestMapping("/ajaxRequest")
    @ResponseBody
    public List<User> ajaxRequest(@RequestBody List<User> userList){
        System.out.println(userList);
        return userList;
    }

    @RequestMapping("/testException")
    public String testException() {
        System.out.println("aaa");
        int i = 1 / 0;
        return "quick";
    }


}
